//
//  GatherPreparing.m
//  LiveGather
//
//  Created by macintosh on 16/9/13.
//  Copyright © 2016年 Yue Shen. All rights reserved.
//

#import "GatherPreparing.h"
#import "UIView+YYAdd.h"

@interface GatherPreparing () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *rtmpTextField;
@property (weak, nonatomic) IBOutlet UIImageView *profile;

@end

@implementation GatherPreparing

- (IBAction)settingBtnClicked:(UIButton *)sender {
    if (_gatherSetting) {
        _gatherSetting();
    }
}

- (IBAction)startLive:(UIButton *)sender {
    NSString *rmptUrl = _rtmpTextField.text;
    if (!rmptUrl.length || ![rmptUrl hasPrefix:@"rtmp://"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"格式错误请重新输入" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [_rtmpTextField becomeFirstResponder];
        }];
        
        [alert addAction:action];
        
        UIViewController *vc = [self viewController];
        [vc presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    if (_startLive) {
        _startLive(rmptUrl);
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self endEditing:YES];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

- (void)setCurrentProfile:(UIImage *)currentProfile
{
    if (_currentProfile != currentProfile) {
        _currentProfile = currentProfile;
        _profile.image = _currentProfile;
    }
}

@end
